using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services.Interfaces;
using System.Threading.Tasks;
using System;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class OtherStockInController : ControllerBase
    {
        private readonly IOtherStockInService _otherStockInService;

        public OtherStockInController(IOtherStockInService otherStockInService)
        {
            _otherStockInService = otherStockInService;
        }

        // GET: api/OtherStockIn
        [HttpGet]
        public async Task<ActionResult<object>> GetOtherStockIns(int page = 1, int pageSize = 10)
        {
            var total = await _otherStockInService.GetTotalOtherStockInsAsync();
            var stockIns = await _otherStockInService.GetOtherStockInsPagedAsync(page, pageSize);
            return Ok(new { items = stockIns, total });
        }

        // GET: api/OtherStockIn/5
        [HttpGet("{id}")]
        public async Task<ActionResult<OtherStockIn>> GetOtherStockIn(int id)
        {
            var stockIn = await _otherStockInService.GetOtherStockInByIdAsync(id);

            if (stockIn == null)
            {
                return NotFound();
            }

            return stockIn;
        }

        // GET: api/OtherStockIn/code/{stockInCode}
        [HttpGet("code/{stockInCode}")]
        public async Task<ActionResult<OtherStockIn>> GetOtherStockInByCode(string stockInCode)
        {
            var stockIn = await _otherStockInService.GetOtherStockInByCodeAsync(stockInCode);

            if (stockIn == null)
            {
                return NotFound();
            }

            return stockIn;
        }

        // PUT: api/OtherStockIn/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutOtherStockIn(int id, OtherStockIn stockIn)
        {
            if (id != stockIn.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _otherStockInService.UpdateOtherStockInAsync(stockIn);
                if (!updated)
                {
                    return NotFound();
                }
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }

            return NoContent();
        }

        // POST: api/OtherStockIn
        [HttpPost]
        public async Task<ActionResult<OtherStockIn>> PostOtherStockIn(OtherStockIn stockIn)
        {
            try
            {
                var createdStockIn = await _otherStockInService.CreateOtherStockInAsync(stockIn);
                return CreatedAtAction("GetOtherStockIn", new { id = createdStockIn.Id }, createdStockIn);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/OtherStockIn/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteOtherStockIn(int id)
        {
            var deleted = await _otherStockInService.DeleteOtherStockInAsync(id);
            if (!deleted)
            {
                return NotFound();
            }

            return NoContent();
        }

        // PATCH: api/OtherStockIn/5/status
        [HttpPatch("{id}/status")]
        public async Task<IActionResult> ChangeOtherStockInStatus(int id, [FromBody] StatusChangeRequest request)
        {
            var success = await _otherStockInService.ChangeOtherStockInStatusAsync(id, request.Status, request.UpdatedBy);
            if (!success)
            {
                return NotFound();
            }

            return NoContent();
        }

        public class StatusChangeRequest
        {
            public string Status { get; set; }
            public int UpdatedBy { get; set; }
        }

        // PATCH: api/OtherStockIn/5/approve
        [HttpPatch("{id}/approve")]
        public async Task<IActionResult> ApproveOtherStockIn(int id, [FromBody] ApproveRequest request)
        {
            try
            {
                if (!int.TryParse(request.ApprovedBy, out int approverId))
                {
                    return BadRequest("审批人ID必须是有效的整数");
                }
                var success = await _otherStockInService.ApproveOtherStockInAsync(id, approverId, request.ApprovalRemark);
                if (!success)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        public class ApproveRequest
        {
            public string ApprovedBy { get; set; }
            public string ApprovalRemark { get; set; }
        }
    }
}